package com.qiaofang.applet.rest;

import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.constants.FirstEstateSaleStautsConstant;
import com.qiaofang.applet.constants.SubscribeConstants;
import com.qiaofang.applet.service.member.domain.MemberSubscribe;
import com.qiaofang.applet.service.member.service.MemberService;
import com.qiaofang.applet.service.member.service.MemberSubscribeService;
import com.qiaofang.applet.service.member.service.dto.MemberSubscribeButtonQueryDto;
import com.qiaofang.applet.service.member.service.dto.MemberSubscribeQueryDto;
import com.qiaofang.applet.service.member.service.dto.MemberSubscribeSaveDto;
import com.qiaofang.applet.service.member.service.dto.MemberSubscribeStatusQueryDto;
import com.qiaofang.applet.service.member.service.mapstruct.MemberSubscribeMapper;
import com.qiaofang.applet.service.property.domain.FirsthandEstate;
import com.qiaofang.applet.service.property.service.FirsthandEstateService;
import com.qiaofang.applet.service.property.service.dto.FirstEstateSubscribeDto;
import com.qiaofang.applet.service.property.service.dto.FirsthandEstateQueryDto;
import com.qiaofang.applet.service.property.service.mapstruct.FirsthandEstateMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author daiyuxaing
 */
@Slf4j
@RestController
@RequestMapping("/api/pcweb/auth/memberSubscribe")
@Api(tags = "会员：开盘提醒")
public class MemberSubscribeController {

    @Autowired
    MemberService membeService;

    @Autowired
    MemberSubscribeService memberSubscribeService;

    @Autowired
    FirsthandEstateService firsthandEstateService;

    @Autowired
    FirsthandEstateMapper firsthandEstateMapper;

    @Autowired
    MemberSubscribeMapper memberSubscribeMapper;

    @PostMapping("/add")
    @ApiOperation(value = "添加开盘提醒", notes = "添加开盘提醒")
    public DataResponse<Object> add(@RequestBody @Valid DataRequest<MemberSubscribeSaveDto> request) {
        request.getParam().setCityCode(QfContextHolder.getContext().getCityCode());
        FirsthandEstate firsthandEstate = firsthandEstateService.findByEstateUniqueUuid(request.getParam().getEstateUniqueUuid());
        request.getParam().setCityCode(firsthandEstate.getCity());
        memberSubscribeService.saveByParams(request.getParam(), SubscribeConstants.ADD_SUBSCRIBE.getValue());

        return DataResponse.ok();
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "取消开盘提醒", notes = "取消开盘提醒")
    public DataResponse<Object> cancel(@RequestBody @Valid DataRequest<MemberSubscribeSaveDto> request) {
        request.getParam().setCityCode(QfContextHolder.getContext().getCityCode());
        FirsthandEstate firsthandEstate = firsthandEstateService.findByEstateUniqueUuid(request.getParam().getEstateUniqueUuid());
        request.getParam().setCityCode(firsthandEstate.getCity());
        memberSubscribeService.saveByParams(request.getParam(), SubscribeConstants.CANCEL_SUBSCRIBE.getValue());

        return DataResponse.ok();
    }


    @PostMapping("/searchSubscribe")
    @ApiOperation(value = "查询开盘提醒列表", notes = "查询开盘提醒列表")
    public DataResponse<Page<FirstEstateSubscribeDto>> searchSubscribe(@RequestBody @Valid PageDataRequest<MemberSubscribeStatusQueryDto> request) {
        Page<FirstEstateSubscribeDto> result = null;

        MemberSubscribeQueryDto memberSubscribeQueryDto = new MemberSubscribeQueryDto();
        memberSubscribeQueryDto.setMemberId(request.getParam().getMemberId());
        memberSubscribeQueryDto.setSubscribeStatus(SubscribeConstants.ADD_SUBSCRIBE.getValue());

        List<MemberSubscribe> memberSubscribeList = memberSubscribeService.findEstateUniqueUuidListByParams(memberSubscribeQueryDto);

        if (!CollectionUtils.isEmpty(memberSubscribeList)) {
            //所有订阅的楼盘uuid
            List<String> uuidList = memberSubscribeList.stream().map(MemberSubscribe::getEstateUniqueUuid).collect(Collectors.toList());

            FirsthandEstateQueryDto queryDto = new FirsthandEstateQueryDto();
            queryDto.setEstateUniqueUuids(uuidList.stream().distinct().collect(Collectors.toList()));

            String saleStatusType = request.getParam().getSaleStatusType();
            if (StringUtils.isNotBlank(saleStatusType)) {
                if (saleStatusType.equals("即将开盘")) {
                    queryDto.setSaleStatus(FirstEstateSaleStautsConstant.SALE_STATUS_1);
                    queryDto.setComingOpen(true);
                } else if (saleStatusType.equals("未开盘")) {
                    queryDto.setSaleStatus(FirstEstateSaleStautsConstant.SALE_STATUS_1);
                } else if (saleStatusType.equals("在售")) {
                    List<String> saleStatusList = new ArrayList<>();
                    saleStatusList.add(FirstEstateSaleStautsConstant.SALE_STATUS_2);
                    saleStatusList.add(FirstEstateSaleStautsConstant.SALE_STATUS_3);
                    saleStatusList.add(FirstEstateSaleStautsConstant.SALE_STATUS_4);
                    saleStatusList.add(FirstEstateSaleStautsConstant.SALE_STATUS_5);
                    queryDto.setSaleStatusList(saleStatusList);
                }
            }

            Page<FirsthandEstate> firsthandEstatePage = firsthandEstateService.findAllByUuids(queryDto,PageUtil.getPageable(request));
            result = firsthandEstateService.domainToPageDto(firsthandEstatePage, memberSubscribeList);
        }
        return DataResponse.ok(result);
    }

    @PostMapping("/getSubscribeButton")
    @ApiOperation(value = "开盘提醒按钮", notes = "开盘提醒按钮")
    public DataResponse<String> getSubscribeButton(@RequestBody @Valid DataRequest<MemberSubscribeButtonQueryDto> request) {
        // 默认按钮状态是隐藏
        String buttonStatus = "0";

        MemberSubscribe memberSubscribe = memberSubscribeService.findByMemberIdAndEstateUniqueUuid(request.getParam().getMemberId(), request.getParam().getEstateUniqueUuid());

        // 开盘状态
        FirsthandEstate firsthandEstate = firsthandEstateService.findByEstateUniqueUuid(request.getParam().getEstateUniqueUuid());

        Boolean openStatus = false;

        if (!FirstEstateSaleStautsConstant.SALE_STATUS_1.equals(firsthandEstate.getSaleStatus())) {
            openStatus = true;
        }

        if (memberSubscribe == null) {
            // 无订阅记录且未开盘状态，按钮显示开盘提醒
            if (!openStatus) {
                buttonStatus = "1";
            }
            // 无订阅记录且未开盘状态，按钮隐藏
        } else {
            if (SubscribeConstants.ADD_SUBSCRIBE.getValue().equals(memberSubscribe.getSubscribeStatus())) {
                //订阅且未开盘状态 按钮显示取消开盘提醒
                //订阅且开盘状态 按钮显示取消开盘提醒
                buttonStatus = "2";
            } else {
                //取消订阅且未开盘状态 按钮显示开盘提醒
                if (!openStatus) {
                    buttonStatus = "1";
                }
                // 取消订阅且开盘状态 按钮隐藏
            }
        }

        return DataResponse.ok(buttonStatus);
    }


}
